package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 别名
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        /*
            查看1年级1班一共多少人?
            SELECT COUNT(*)
            FROM class c
            JOIN student s ON c.id=s.class_id
            WHERE c.name='1年级1班'
         */

        try (
                Connection connection = DButil.getConnection();//获取连接
        ){
            Statement statement = connection.createStatement();//获取Statement对象
            String sql = "SELECT COUNT(*) count " +
                    "FROM class c  " +
                    "JOIN student s ON c.id=s.class_id " +
                    "WHERE c.name='1年级1班' ";

            ResultSet rs = statement.executeQuery(sql);//执行查询
            while (rs.next()){//遍历结果集
                int count = rs.getInt("count");
                System.out.println("人数:"+count);
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
